package com.fudan.es.aggs.response.handler;

import java.util.Map;

import org.elasticsearch.search.aggregations.Aggregations;
import org.springframework.beans.factory.annotation.Autowired;

import com.fudan.es.aggs.ConstString;
import com.fudan.es.aggs.ResponseParser;
import com.fudan.es.entity.MetricPack;
import com.fudan.es.entity.MetricPack.MetricType;
import com.fudan.framework.utils.SpringUtils;

public abstract class ResponseHandler<T extends MetricPack> {

	protected static final String ROOT_PATH = ConstString.ROOT_PATH;
	protected static final String DEFAULT_DOC = ConstString.DEFAULT_DOC;

	protected static final String SUM_TAG = ConstString.SUM_TAG;
	protected static final String AVG_TAG = ConstString.AVG_TAG;
	protected static final String TERMS_TAG = ConstString.TERMS_TAG;

	@Autowired
	protected ResponseParser responseParser;

	abstract public Map<String,Object> handle(Map<String,Object> map,Aggregations aggregations,T metricPack);

	public static ResponseHandler buildHandler(MetricType type) {
		switch (type) {
		case TERMS:
			return get("termsResponse");
		case AVG:
			return get("avgResponse");
		case SUM:
			return get("sumResponse");
		case DATE:
			return get("dateResponse");
		default:
			return null;
		}
	}

	protected String getAggregationName(MetricPack metricPack) {
		return metricPack.getType().toString()+"$"+metricPack.getField().getId();
	}

	public static ResponseHandler get(String name) {
		return (ResponseHandler) SpringUtils.getBean(name);
	}
}
